var CookieHandler = {
	
	checkUser : function() 
	{
		var c = Common.Cookies.readCookie("myfeed_cookie");
		// se il cookie è presente
		if(c != null) {
			// rimuovo il form di login
			/*var navbar = document.getElementById("navbar");
			navbar.removeChild(navbar.getElementsByTagName("form")[0]);
			// faccio comparire il link ai propri feed
			var feedsLink = document.createElement("a");
			feedsLink.innerHTML = "Welcome " + c + "<br/>" + "Go to your feeds!";
			feedsLink.href= "main_app.html?id=" + c;
			feedsLink.id="your_feeds_link";
			navbar.insertBefore(feedsLink, document.getElementById("nav_list"));
			*/
			// test
			Common.Cookies.eraseCookie("myfeed_cookie");
		}	
		// se il cookie non c'è non faccio niente
		else {
			// per test
			//Common.Cookies.createCookie("myfeed_cookie", "oscar", 7); 
			
		}
	}


};




// Literal per contenere i metodi che parsano l'xml dei most feed
var HomeSubsLoader = 
{
    browser : Ajax.browser(),
    loadMosts : function () 
    {
        var XHR = null;
        try {
            XHR = Ajax.createXMLHttpRequest();
        }
        catch (e) 
        {
            document.write("<p class=\"error\">It seems this browser can't handle some dynamic script. Try visit <a href=\"./xml/feedindex.xml\">our board</a> to get the same result</p>");
        }
        var loader = this;
        //alert(XHR);
        if (XHR != null) 
        {
            //alert("request created");
            XHR.open("get", "./xml/feedindex.xml", true);
            XHR.onreadystatechange = function () 
            {
                if (XHR.readyState == 4) 
                {
                    if (XHR.status == 200) {
                        if (loader.browser == "MOD") {
                            parseData(XHR.responseText);
                        }
                    }
                    if (XHR.status == 404) 
                    {
                        if (loader.browser == "MOD") 
                        {
                            var cont = document.getElementById("most_popular_feed");
                            var par = document.createElement("p");
                            par.setAttribute("class", "error");
                            par.innerHTML = "Errore nella ricezione dei dati!";
                            cont.appendChild(par);
                        }
                    }
                }
            }
            XHR.setRequestHeader("Content-Type", "text/xml");
            XHR.setRequestHeader("Cache-Control", "no-cache, must-revalidate");
            XHR.send(null);
        }
        function parseData(sXml) 
        {
            var xmlDom = Ajax.createDocument();
            var root = null;
            try 
            {
                var p = new DOMParser();
                xmlDom = p.parseFromString(sXml, "text/xml");
                root = xmlDom.documentElement;
            }
            catch (e) {
                xmlDom.loadXML(sXml);
                root = xmlDom.documentElement;
            }
            var mosts = root.getElementsByTagName("most");
            // costruzione della lista most_popular
            for (var j = 0; j < mosts.length; j++) 
            {
                //
                // CARICAMENTO DATI PER MOST POPULAR FEED
                //
                if (mosts[j].getAttribute("id") == "most_popular_feed") 
                {
                    var list = document.createElement("ol");
                    list.className = "feed_post_list_index";
                    document.getElementById("most_popular_feed").appendChild(list);
                    var feeds = mosts[j].childNodes;
                    for (var i = 0; i < feeds.length; i++) 
                    {
                        //alert(feeds[i]);
                        if (feeds[i].tagName == "feed") 
                        {
                            var li = document.createElement("li");
                            var h4 = document.createElement("h4");
                            var span = document.createElement("span");
                            var par = document.createElement("p");
                            var a = document.createElement("a");
                            var theLink, theTitle, thePar;
                            var currentChild = feeds[i].firstChild;
                            do 
                            {
                                switch (currentChild.tagName) 
                                {
                                    case "name" :
                                        theTitle = currentChild.firstChild.nodeValue;
                                    case "link" :
                                        theLink = currentChild.firstChild.nodeValue;
                                    case "last" :
                                        thePar = currentChild.firstChild.nodeValue + " ";
                                }
                                //currentChild = currentChild.nextSibling;
                            }
                            while (currentChild = currentChild.nextSibling) {
                                a.setAttribute("href", theLink);
                            }
                            a.appendChild(document.createTextNode("read all"));
                            h4.appendChild(document.createTextNode(theTitle));
                            span.appendChild(document.createTextNode("published"));
                            par.appendChild(document.createTextNode(thePar));
                            par.appendChild(a);
                            li.appendChild(h4);
                            li.appendChild(span);
                            li.appendChild(par);
                            list.appendChild(li);
                        }
                    }
                }
                //
                // CARICAMENTO DATI PER MOST RECENT FEED
                // 
                if (mosts[j].getAttribute("id") == "most_recent_feed") 
                {
                    var list = document.createElement("ol");
                    list.className = "feed_post_list_index";
                    document.getElementById("most_recent_feed").appendChild(list);
                    var feeds = mosts[j].childNodes;
                    for (var i = 0; i < feeds.length; i++) 
                    {
                        //alert(feeds[i]);
                        if (feeds[i].tagName == "feed") 
                        {
                            var li = document.createElement("li");
                            var h4 = document.createElement("h4");
                            var span = document.createElement("span");
                            var par = document.createElement("p");
                            var a = document.createElement("a");
                            var theLink, theTitle, thePar;
                            var currentChild = feeds[i].firstChild;
                            do 
                            {
                                switch (currentChild.tagName) 
                                {
                                    case "name" :
                                        theTitle = currentChild.firstChild.nodeValue;
                                    case "link" :
                                        theLink = currentChild.firstChild.nodeValue;
                                    case "last" :
                                        thePar = currentChild.firstChild.nodeValue + " ";
                                }
                                //currentChild = currentChild.nextSibling;
                            }
                            while (currentChild = currentChild.nextSibling) {
                                a.setAttribute("href", theLink);
                            }
                            a.appendChild(document.createTextNode("read all"));
                            h4.appendChild(document.createTextNode(theTitle));
                            span.appendChild(document.createTextNode("published"));
                            par.appendChild(document.createTextNode(thePar));
                            par.appendChild(a);
                            li.appendChild(h4);
                            li.appendChild(span);
                            li.appendChild(par);
                            list.appendChild(li);
                        }
                    }
                }
            }
        }
    }
}
